package org.hzy.services.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.hzy.dao.IEmpDao;
import org.hzy.dao.ITLog;
import org.hzy.dao.ITUserDao;
import org.hzy.entity.Emp;
import org.hzy.entity.TLog;
import org.hzy.entity.TUser;
import org.hzy.md5.MD5;
import org.hzy.services.IUserLoginService;
import org.hzy.tools.JsonDateValueProcessor;
import org.hzy.tools.JsonUtil;
import org.hzy.tools.PageBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserLoginService implements IUserLoginService{
	private ITUserDao iu;
	private ITLog it;
//	private static Logger l=Logger.getLogger(LoginLogg.class);
	private IEmpDao ie;
	public IEmpDao getIe() {
		return ie;
	}
	public void setIe(IEmpDao ie) {
		this.ie = ie;
	}
	public ITLog getIt() {
		return it;
	}
	public void setIt(ITLog it) {
		this.it = it;
	}
	public ITUserDao getIu(){
		return iu;
	}
	public void setIu(ITUserDao iu) {
		this.iu = iu;
	}
	
	public TUser getUserByName(String name,String pwd) {
		// TODO Auto-generated method stub
		TUser tu=iu.get(name);
		if(tu!=null){
			if(tu.getUpwd().equals(MD5.getMD5(pwd))){
//				TLog tl=new TLog();
//				tl.setLtime(new Date());
//				tl.setTUser(tu);
//				it.save(tl);
				return tu;
			}
		}
		return null;
	}
	public Integer getCount(){
		return ie.getCount();
	}
	public List<Emp> findEmpAll(Integer pageIndex,Integer pageSize){
		return ie.findAllByPage(pageIndex, pageSize);
	}
	
	public PageBean getPageBean(Integer start,Integer limit){
		String res=null;
		PageBean page=new PageBean();
		try {
			Integer count=getCount();
			limit=limit>=count?count:limit;
			Integer total=count % limit == 0? count / limit: (count / limit)+1;
			Integer pageIndex=(start/limit)+1;
			pageIndex=pageIndex>total?total:pageIndex;
			List<Emp> list=findEmpAll(start, limit);
			Integer upPageIndex=pageIndex-1<=0?1:pageIndex-1;
			Integer downPageIndex=pageIndex+1>=total?total:pageIndex+1;
//			page=new PageBean(pageIndex,upPageIndex,downPageIndex,count,total,limit,list);
			page.setCurrentPage(pageIndex);
			page.setData(list);
			page.setNextPage(downPageIndex);
			page.setPrevPage(upPageIndex);
			page.setTotal(total);
			page.setPageSize(limit);
			page.setPageCount(count);

		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("!!!!!!!!!!!!!!!!!!!");
			res="00001";
			page.setResult(res);
		}
		return page;
	}
	
	
	public static void main(String[] args){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("beans1.xml");
		IUserLoginService tu=ctx.getBean("UserLoginService",IUserLoginService.class);
		System.out.println(tu.getUserByName("张三", "123").getUname());
	}
	
	public boolean addUser(TUser tu){
		// TODO Auto-generated method stub
		iu.save(tu);
		return true;
	}
	public void addLog(TLog tl) {
		// TODO Auto-generated method stub
		it.save(tl);
	}
}
